<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mongoose | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="mongoosemongoose 是node操作mongodb的一个插件
下载安装1npm i mongoose -S
连接数据库useNewUrlParser  使用新的url解析器useUnifiedTopology 使用统一拓扑结构useCreateIndex 使用创建索引  只有创建索引后面">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="mongoose"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>mongoose</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/30/mongoose/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-30T12:15:41.000Z">
          2021-01-30
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="mongoose"><a href="#mongoose" class="headerlink" title="mongoose"></a>mongoose</h2><p>mongoose 是node操作mongodb的一个插件</p>
<h3 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i mongoose -S</span><br></pre></td></tr></table></figure>
<h3 id="连接数据库"><a href="#连接数据库" class="headerlink" title="连接数据库"></a>连接数据库</h3><p>useNewUrlParser  使用新的url解析器<br>useUnifiedTopology 使用统一拓扑结构<br>useCreateIndex 使用创建索引  只有创建索引后面才可以让某个字段唯一</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const mongoose=require(<span class="string">&quot;mongoose&quot;</span>);</span><br><span class="line">const Schema=mongoose.Schema;</span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://localhost/game&#x27;</span>,&#123;useNewUrlParser:<span class="literal">true</span>,useUnifiedTopology:<span class="literal">true</span>,useCreateIndex:<span class="literal">true</span>&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h3><p>如果字段仅仅是简单结构，直接设置<br>但是如果遇到复杂字段，例如下面的name除了需要设置type类型外，还需要设置unique标识唯一<br>如果属性是一个对象和数组，可以写成items这个样子</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">const mongoose=require(<span class="string">&quot;mongoose&quot;</span>);</span><br><span class="line">const Schema=mongoose.Schema;</span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://localhost/game&#x27;</span>,&#123;useNewUrlParser:<span class="literal">true</span>,useUnifiedTopology:<span class="literal">true</span>,useCreateIndex:<span class="literal">true</span>&#125;);</span><br><span class="line"></span><br><span class="line">var  PackData=new Schema(&#123;</span><br><span class="line">    name:&#123;<span class="built_in">type</span>:String,unique:<span class="literal">true</span>,index:<span class="literal">true</span>&#125;,</span><br><span class="line">    num:Number,</span><br><span class="line">    items:[</span><br><span class="line">        &#123;name:String,num:Number&#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br><span class="line">var Pack=mongoose.model(<span class="string">&quot;pack&quot;</span>,PackData);</span><br></pre></td></tr></table></figure>
<h3 id="给集合中插入一条信息"><a href="#给集合中插入一条信息" class="headerlink" title="给集合中插入一条信息"></a>给集合中插入一条信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var t=new Pack(&#123;</span><br><span class="line">    name:<span class="string">&quot;a3&quot;</span>,</span><br><span class="line">    num:2,</span><br><span class="line">    items:[</span><br><span class="line">        &#123;name:<span class="string">&quot;b3&quot;</span>,num:1&#125;,</span><br><span class="line">        &#123;name:<span class="string">&quot;b4&quot;</span>,num:2&#125;,</span><br><span class="line">        &#123;name:<span class="string">&quot;b5&quot;</span>,num:3&#125;,</span><br><span class="line">        &#123;name:<span class="string">&quot;b6&quot;</span>,num:4&#125;,</span><br><span class="line">    ]&#125;);</span><br><span class="line">    t.save(&#123;&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="同时插入多条数据"><a href="#同时插入多条数据" class="headerlink" title="同时插入多条数据"></a>同时插入多条数据</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">var arr = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&quot;a4&quot;</span>,</span><br><span class="line">        num: 3,</span><br><span class="line">        items: [</span><br><span class="line">            &#123; name: <span class="string">&quot;b4&quot;</span>, num: 1 &#125;,</span><br><span class="line">            &#123; name: <span class="string">&quot;b5&quot;</span>, num: 2 &#125;,</span><br><span class="line">            &#123; name: <span class="string">&quot;b6&quot;</span>, num: 3 &#125;,</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&quot;a5&quot;</span>,</span><br><span class="line">        num: 6,</span><br><span class="line">        items: [</span><br><span class="line">            &#123; name: <span class="string">&quot;b7&quot;</span>, num: 1 &#125;,</span><br><span class="line">            &#123; name: <span class="string">&quot;b5&quot;</span>, num: 2 &#125;,</span><br><span class="line">            &#123; name: <span class="string">&quot;b6&quot;</span>, num: 3 &#125;,</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">Pack.insertMany(arr,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="修改数据"><a href="#修改数据" class="headerlink" title="修改数据"></a>修改数据</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Pack.updateOne(&#123;name:<span class="string">&quot;a5&quot;</span>&#125;,&#123;<span class="variable">$set</span>:&#123;num:12&#125;&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">    Pack.find(&#123;&#125;,<span class="keyword">function</span>(error,data)&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Pack.deleteOne(&#123;name:<span class="string">&quot;a5&quot;</span>&#125;,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">    Pack.find(&#123;&#125;,<span class="keyword">function</span>(error,data)&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">       &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="查找数据"><a href="#查找数据" class="headerlink" title="查找数据"></a>查找数据</h3><h4 id="查找所有"><a href="#查找所有" class="headerlink" title="查找所有"></a>查找所有</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Pack.find(&#123;&#125;,<span class="keyword">function</span>(error,data)&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line"> &#125;)</span><br></pre></td></tr></table></figure>
<h4 id="查找某个"><a href="#查找某个" class="headerlink" title="查找某个"></a>查找某个</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Pack.find(&#123;name:<span class="string">&quot;a1&quot;</span>&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="查找条件"><a href="#查找条件" class="headerlink" title="查找条件"></a>查找条件</h4><p>$get:2  大于等于2的<br>$lte:2  小于等于2的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Pack.find(&#123;num:&#123;<span class="variable">$gte</span>:2&#125;&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br><span class="line">Pack.find(&#123;num:&#123;<span class="variable">$lte</span>:2&#125;&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br><span class="line">//正则表达式判断</span><br><span class="line">Pack.find(&#123;name:/^\w\d$/&#125;,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>